<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学生校号" prop="tgsqXsxh">
        <el-input
          v-model="queryParams.tgsqXsxh"
          placeholder="请输入学生校号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生姓名" prop="tgsqXsxm">
        <el-input
          v-model="queryParams.tgsqXsxm"
          placeholder="请输入学生姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属学院" prop="tgsqSsxy">
        <el-input
          v-model="queryParams.tgsqSsxy"
          placeholder="请输入所属学院"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所学专业" prop="tgsqSxzy">
        <el-input
          v-model="queryParams.tgsqSxzy"
          placeholder="请输入所学专业"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="原学社" prop="tgsqYxsmc">
        <el-input
          v-model="queryParams.tgsqYxsmc"
          placeholder="请输入原学社"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="调整后学社" prop="tgsqXxsmc">
        <el-input
          v-model="queryParams.tgsqXxsmc"
          placeholder="请输入调整后学社"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['admin:tgsq:add']"
        >新增</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['admin:tgsq:edit']"
        >审批</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['admin:tgsq:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tgsqList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学生校号" align="center" prop="tgsqXsxh" />
      <el-table-column label="学生姓名" align="center" prop="tgsqXsxm" />
      <el-table-column label="所属学院" align="center" prop="tgsqSsxy" >
        <template slot-scope="scope">
          {{ getdeptLabel(scope.row.tgsqSsxy) }}
        </template>
      </el-table-column>
      <el-table-column label="所学专业" align="center" prop="tgsqSxzy" />
      <el-table-column label="原学社" align="center" prop="tgsqYxsmc" >
        <template slot-scope="scope">
        {{ getxsxxLabel(scope.row.tgsqYxsmc) }} 
        </template>
      </el-table-column>
      <el-table-column label="学社类别" align="center" prop="tgsqXslb" >
        <template slot-scope="scope">
        {{ getLabel(scope.row.tgsqXslb) }} 
        </template>
      </el-table-column>
      <el-table-column label="调整后学社" align="center" prop="tgsqXxsmc" >
        <template slot-scope="scope">
        {{ getxsxxLabel(scope.row.tgsqXxsmc) }} 
        </template>
      </el-table-column>
      <el-table-column label="原岗位等级" align="center" prop="tgsqYgwdj" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.xveshe_grade" :value="scope.row.tgsqYgwdj"/>
        </template>
      </el-table-column>
      <el-table-column label="原岗位名称" align="center" prop="tgsqYgwmc" >
        <template slot-scope="scope">
        {{ getgwxxLabel(scope.row.tgsqYgwmc) }} 
        </template>
      </el-table-column>
      <el-table-column label="调整后岗位等级" align="center" prop="tgsqXgwdj" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.xveshe_grade" :value="scope.row.tgsqXgwdj"/>
        </template>
      </el-table-column>
      <el-table-column label="调整后岗位名称" align="center" prop="tgsqXgwmc" >
        <template slot-scope="scope">
        {{ getgwxxLabel(scope.row.tgsqXgwmc) }} 
        </template>
      </el-table-column>
      <el-table-column label="审批结果" align="center" prop="tgsqSpjg"> 
        <template slot-scope="scope">
          {{ getsplabel(scope.row.tgsqSpjg) }}
        </template>
      </el-table-column>
      <el-table-column label="审批意见" align="center" prop="tgsqSpyj" > 
        <template slot-scope="scope">
          {{ getsplabel(scope.row.tgsqSpyj) }}
        </template>
      </el-table-column>
      <el-table-column label="审批人-原学社导师" align="center" prop="tgsqYxsds" > 
        <template slot-scope="scope">
          {{ getTchLabel(scope.row.tgsqYxsds) }}
        </template>
      </el-table-column>
      
      <el-table-column label="原学社导师审批意见" align="center" prop="tgsqYspyj" > 
        <template slot-scope="scope">
          {{ getsplabel(scope.row.tgsqYspyj) }}
        </template>
      </el-table-column>
      <el-table-column label="原学社导师审批时间" align="center" prop="tgsqYxsdsSj" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.tgsqYxsdsSj, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审批人-学社导师" align="center" prop="tgsqXsds" > 
        <template slot-scope="scope">
          {{ getTchLabel(scope.row.tgsqXsds) }}
        </template>
      </el-table-column>
      <el-table-column label="学社导师审批时间" align="center" prop="tgsqXsdsSj" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.tgsqXsdsSj, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="学社简介" align="center" prop="tgsqXxsmcXsjj" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['admin:tgsq:edit']"
          >审批</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改调岗调社申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学社审批意见" prop="tgsqSpyj" v-if="sp">
          <el-select v-model="form.tgsqSpyj" placeholder="请输入审批意见" clearable >
          <el-option
            v-for="dict in dict.type.shexve_sp"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
        </el-form-item>
        <el-form-item label="原学社审批意见" prop="tgsqYspyj" v-if="ysp">
          <el-select v-model="form.tgsqYspyj" placeholder="请输入审批意见" clearable >
          <el-option
            v-for="dict in dict.type.shexve_sp"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTgsq, getTgsq, delTgsq, addTgsq, updateTgsq } from "@/api/admin/tgsq";
import { listTstg, getTstg, updateTstg } from "@/api/admin/tstg";
import {getUser} from "@/api/system/user";
import { allocatedUserList } from "@/api/system/role";
import {listDeptById} from "@/api/system/dept";
import {listXslb}from "@/api/admin/xslb"
import {listXsxx,getXsxx}from "@/api/admin/xsxx"
import {listGwxx,getGwxx}from "@/api/admin/gwxx"
import store from "@/store";
export default {
  name: "Tgsq",
  dicts: ['xveshe_enterprise', 'xveshe_specialty', 'shexve_sp','xveshe_grade', 'xveshe_status'],
  data() {
    return {
      //审批项
      ysp: false,
      //审批项
      sp: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      //学院信息
      deptList:[],
      //学社类别
      xslbList:[],
      //岗位信息
      listGwxx:[],
      //学社信息
      xsxxList:[],
      //教师信息
      tchList:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 调岗调社申请表格数据
      tgsqList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tgsqXsxh: null,
        tgsqXsxm: null,
        tgsqSsxy: null,
        tgsqSxzy: null,
        tgsqYxsmc: null,
        tgsqXslb: null,
        tgsqXxsmc: null,
        tgsqYgwdj: null,
        tgsqYgwmc: null,
        tgsqXgwdj: null,
        tgsqXgwmc: null,
        tgsqSpjg: null,
        tgsqSpyj: null,
        creator: null,
        createdate: null,
        lastmodifier: null,
        lastmodifieddate: null,
        tgsqYxsds: null,
        tgsqYxsdsSj: null,
        tgsqXsds: null,
        tgsqXsdsSj: null,
        tgsqXxsmcXsjj: null
      },
      // 表单参数
      form: {},
      tsform:{},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDept(201);
    this.getlistGwxx();
    this.getXsxxList();
    this.getxslb();
    this.getTch();
  },
  methods: {
    //审批显示项
    spopen(){
      if(store.getters.name===this.form.tgsqYxsds)
      this.sp=true
      else if(store.getters.name===this.form.tgsqXsds)
      this.ysp=true
    },
    getsplabel(value){
				 if(value==='0')
				return '通过'
				else if(value==='1')
				return '已驳回'
				else
				return '待审批'
			},
    /** 查询调岗调社申请列表 */
    getList() {
      this.loading = true;
      listTgsq(this.queryParams).then(response => {
        this.tgsqList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tgsqXsxh: null,
        tgsqXsxm: null,
        tgsqSsxy: null,
        tgsqSxzy: null,
        tgsqYxsmc: null,
        tgsqXslb: null,
        tgsqXxsmc: null,
        tgsqYgwdj: null,
        tgsqYgwmc: null,
        tgsqXgwdj: null,
        tgsqXgwmc: null,
        tgsqSpjg: null,
        tgsqSpyj: null,
        creator: null,
        createdate: null,
        lastmodifier: null,
        lastmodifieddate: null,
        tgsqYxsds: null,
        tgsqYxsdsSj: null,
        tgsqXsds: null,
        tgsqXsdsSj: null,
        tgsqXxsmcXsjj: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加调岗调社申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTgsq(id).then(response => {
        this.form = response.data;
        if(this.form.tgsqSpjg==='0'){ 
              this.$modal.msgSuccess("审核已通过，无需再次审核");
              return
            }
        this.spopen()
        this.open = true;
        this.title = "修改调岗调社申请";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            if(this.form.tgsqYspyj==='0'&&this.form.tgsqSpyj==='0')
            {
              this.form.tgsqSpjg='0'
            }
            updateTgsq(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.updateXveshe();
              this.open = false;
              this.getList();
            });
          } else {
            addTgsq(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除调岗调社申请编号为"' + ids + '"的数据项？').then(function() {
        return delTgsq(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('admin/tgsq/export', {
        ...this.queryParams
      }, `tgsq_${new Date().getTime()}.xlsx`)
    },
    //修改学生调社调岗
    updateXveshe(){
      const queryParams= {
        tstgXsxh: this.form.tgsqXsxh
      }
      listTstg(queryParams).then(response => {
        this.tsform.id = response.rows[0].id;
        this.tsform.tstgXsxh=this.form.tgsqXsxh;
        this.tsform.tstgSsxy=this.form.tgsqSsxy;
        this.tsform.tstgSxzy=this.form.tgsqSxzy;
        this.tsform.tstgXslb=this.form.tgsqXslb;
        this.tsform.tstgXsmc=this.form.tgsqXxsmc;
        this.tsform.tstgGwmc=this.form.tgsqXgwmc;
        this.tsform.tstgGwdj=this.form.tgsqXgwdj;
     
        updateTstg(this.tsform).then(response => {
          console.log(tsform);
            });
      });
    },
    /** 查询学社类别 */
    getxslb() {
      this.loading = true;
      listXslb(this.queryParams).then(response => {
        this.xslbList = response.rows;
      });
    },
    //获取学社信息
    getXsxxList(){
      listXsxx().then(response => {
        this.xsxxList = response.rows;
      });
    },
    //获取岗位信息
    getlistGwxx(){
      listGwxx().then(response => {
        this.listGwxx = response.rows;
      });
    }, 
    //查询部门/学院信息
    getDept(id){
      listDeptById(id).then(response => {
        this.deptList = response.data;
      });
    }, 
    //获取教师列表
    getTch(){
      const queryParams= {
        roleId: 101
      }
      allocatedUserList(queryParams).then(res => {
        this.tchList = res.rows;
      });
    },
    getLabel(id) {  
      // 创建一个映射函数，该函数将ID映射到标签  
      const map = this.xslbList.reduce((acc, item) => {  
        acc[item.id] = item.xsglXslb;  
        return acc;  
      }, {});  
      // 返回一个函数，该函数接受一个ID并返回相应的标签  
      return map[id] || 'Unknown Label';  
    }  ,
    getgwxxLabel(id) {  
      // 创建一个映射函数，该函数将ID映射到标签  
      const map = this.listGwxx.reduce((acc, item) => {  
        acc[item.id] = item.gwxxGwmc;
        return acc;  
      }, {});  
      // 返回一个函数，该函数接受一个ID并返回相应的标签  
      return map[id] || 'Unknown Label';  
    }   ,
    getxsxxLabel(id) {  
      // 创建一个映射函数，该函数将ID映射到标签  
      const map = this.xsxxList.reduce((acc, item) => {  
        acc[item.id] = item.xsxxXsmc;
        return acc;  
      }, {});  
      // 返回一个函数，该函数接受一个ID并返回相应的标签  
      return map[id] || 'Unknown Label';  
    }  ,
    getdeptLabel(id) {  
      const map = this.deptList.reduce((acc, item) => {  
        acc[item.deptId] = item.deptName;
        return acc;  
      }, {});  
      return map[id] || 'Unknown Label';  
    }   ,
    getTchLabel(userName) {  
      // 创建一个映射函数，该函数将ID映射到标签  
      const map = this.tchList.reduce((acc, item) => {  
        acc[item.userName] = item.nickName;
        return acc;  
      }, {});  
      // 返回一个函数，该函数接受一个ID并返回相应的标签  
      return map[userName] || 'Unknown Label';  
    }   
  }
};
</script>
